package com.shangcheng.dubbo.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shangcheng.dubbo.mappers.PmsSkuStockMapper;
import com.shangcheng.model.domain.PmsProductAttributeValue;
import com.shangcheng.model.domain.PmsSkuStock;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class PmsSkuStockApiImpl implements PmsSkuStockApi {
    @Autowired
    private PmsSkuStockMapper skuStockMapper;

    @Override
    public void save(List<PmsSkuStock> skuStockList, Long id) {
        if (ObjectUtil.isNotEmpty(skuStockList)) {
            for (PmsSkuStock pmsSkuStock : skuStockList) {
                PmsSkuStock one = skuStockMapper.selectOne(new LambdaQueryWrapper<PmsSkuStock>().eq(PmsSkuStock::getSkuCode, pmsSkuStock));
                if (ObjectUtil.isNotEmpty(one)) {
                    throw new RuntimeException("该sku编码已存在");
                }
                pmsSkuStock.setProductId(id);
                skuStockMapper.insert(pmsSkuStock);
            }
        }
    }

    @Override
    public List<PmsSkuStock> findByProductId(Integer id) {
        return skuStockMapper.selectList(new LambdaQueryWrapper<PmsSkuStock>().eq(PmsSkuStock::getProductId, id));
    }

    @Override
    public void updateById(Integer id, List<PmsSkuStock> skuStockList) {
        skuStockMapper.delete(new LambdaQueryWrapper<PmsSkuStock>().eq(PmsSkuStock::getProductId, id));
        this.save(skuStockList, Convert.toLong(id));
    }

    @Override
    public List<PmsSkuStock> findByPidOrCode(Integer pid, String keyword) {
        LambdaQueryWrapper<PmsSkuStock> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(pid), PmsSkuStock::getProductId, pid);
        wrapper.like(StrUtil.isNotBlank(keyword), PmsSkuStock::getSkuCode, keyword);
        return skuStockMapper.selectList(wrapper);
    }
}
